<?php
/*!
 * LostKids API PHP Library v0.1 
 * http://huali.li/
 *
 * Copyright 2011, 
 * Written By James Pang
 *  
 * Date: 2011/2/14
 */

/**
 * represents a weibo msg.
 * Type of msgs includes
 * <li>REPORT: register photo, for reporting</li>
 * <li>SEEK: register photo, for seeking</li>
 * <li>MATCH: identify matchs, matchs/not</li>
 * <li>CONFIRM: confirm final matchs, kid FOUND, GREAT!</li>
 */
class WeiboMessage
{
    const TYPE_REPORT = 'REPORT';
    const TYPE_SEEK = 'SEEK';
    const TYPE_MATCH = 'MATCH';
    const TYPE_NON_MATCH = 'NONMATCH';
    const TYPE_CONFIRM = 'CONFIRM';

    /** new msg */
    const ST_NEW = 'new';
    /** msg hungs for user feedback */
    const ST_HUNG = 'hung';
    /** msg processed */
    const ST_DONE = 'done';
    /** msg aborted */
    const ST_ABORT = 'abort';

    /** local db id */
    public $id;
    private $msgId; // convert to string, prevent long type overflow
    /** matched kid id */
    private $matchId;
    /** diff msg with same batchKey as same person */
    private $batchKey;
    /** register or match */
    private $type;
    /** is seek or report */
    private $isSeek;
    private $imgUrl;
    private $content;
    /** user who created the msg */
    private $ownerId;
    /** data src key from where the msg comes */
    private $dataSrcKey;

    /** msg processing status */
    public $status = self::ST_NEW;

    function __construct() 
    { 
        $a = func_get_args(); 
        $i = func_num_args(); 
        if (method_exists($this,$f='__construct'.$i)) { 
            call_user_func_array(array($this,$f),$a); 
        } 
    }

    /**
     * @param string $dataSrcKey
     * @param string $ownerId, msg owner
     * @param string $msgId, msg id
     * @param string $batchKey, diff msg with same batchKey as same person
     * @param string $imgUrl
     * @param string $type, msg type: 'REGISTER' or 'MATCH', or 'CONFIRM'
     * @param int $matchId, matched kid uid
     */
    public function __construct9($dataSrcKey, $ownerId, $msgId, $batchKey, $imgUrl,
            $type, $isSeek, $content, $matchId) {
        $this->dataSrcKey = $dataSrcKey;
        $this->ownerId = $ownerId;
        $this->msgId = 'm'.$msgId;
        $this->batchKey = $batchKey;
        $this->imgUrl = $imgUrl;
        $this->type = $type;
        $this->isSeek = $isSeek;
        $this->content = $content;
        $this->matchId = $matchId;
    }

    public function __construct1($msg1) {
        $this->id = @$msg1->id;
        $this->dataSrcKey = @$msg1->dataSrcKey;
        $this->ownerId = @$msg1->ownerId;
        $this->msgId = @$msg1->msgId;
        $this->batchKey = @$msg1->batchKey;
        $this->imgUrl = @$msg1->imgUrl;
        $this->type = @$msg1->type;
        $this->isSeek = @$msg1->isSeek;
        $this->content = @$msg1->content;
        $this->matchId = @$msg1->matchId;
    }

    /**
     * generate userId used in faceDB
     */
    public function genUserId() {
    	return $this->dataSrcKey.$this->id;
    }

    public function getDataSrcKey() {
    	return $this->dataSrcKey;
    }

    public function getOwnerId() {
    	return $this->ownerId;
    }

    public function getBatchKey() {
    	return $this->batchKey;
    }

    public function getMsgId() {
    	return substr($this->msgId, 1);
    }

    public function getImgUrl() {
        return $this->imgUrl;
    }

    public function getMsgType() {
        return $this->type;
    }

    public function isSeek() {
        return $this->isSeek;
    }

    public function getContent() {
        return $this->content;
    }

    public function getMatchId() {
        return $this->matchId;
    }

    public function setMatchId($matchId) {
        return $this->matchId = $matchId;
    }
}

/**
 * a batch of msgs, but treated as one WeiboMessage.
 * a batch is with same dataSrc, owner, type, batchKey
 * 
 * @see DataManager->getBatchMsgs($msg)
 */
class BatchMessage extends WeiboMessage {
    /** msg local ids */
	public $ids;
    /** array of imgUrls */
	public $imgUrls;


    function __construct() 
    { 
        $a = func_get_args(); 
        $i = func_num_args(); 
        if (method_exists($this,$f='__construct'.$i)) { 
            call_user_func_array(array($this,$f),$a); 
        } 
    }

    public function __construct0() {
    	$ids = array();
    	$imgUrls = array();
    }

    public function __construct1($msg0) {
        parent::__construct1($msg0);
    	$this->ids[] = @$msg0->id;
    	$this->imgUrls[@$msg0->id] = @$msg0->getImgUrl();
    }

    /**
     * for non-batch, return lid
     */
    function getNormBatchKey() {
        self::genNormBatchKey($this);
    }

    static function genNormBatchKey($m) {
        return $m->getBatchKey() ? Utils::implodeKey($m->getDataSrcKey()
                , $m->getOwnerId(), $m->getMsgType(), $m->getBatchKey())
                 : $m->id;
    }

    function addBatch($msg) {
        if($msg->getBatchKey() == null)
            return false;
        if(isset($parent->id) && ($msg->id == $parent->id)) // duplicate
            return true;
    	if($msg->getDataSrckey() != parent::getDataSrckey())
            return false;
    	if($msg->getOwnerId() != parent::getOwnerId())
            return false;
    	if($msg->getMsgType() != parent::getMsgType())
            return false;
    	if($msg->getBatchKey() != parent::getBatchKey())
            return false;

        if($msg->getMatchId() && parent::getMatchId()
                && $msg->getMatchId() != parent::getMatchId())
            throw new BizException('ERROR: The same batch has diff matchIds, batchKey='
                    .parent::getBatchKey());

        $this->ids[] = $msg->id;
        $this->imgUrls[$msg->id] = $msg->getImgUrl();
        return true;
    }
}
?>
